package com.guigu.www.luolujin.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.luolujin.Service.LljConstitutionTestService;
import com.guigu.www.mapper.*;
import com.guigu.www.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者 罗露金
 */
@Service
public class LljConstiutionTestServiceImpl implements LljConstitutionTestService {
    @Autowired
    ConstitutionTestMapper mapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    RubricMapper rubricMapper;
    @Autowired
    RubricOptionMapper optionMapper;
    @Autowired
    UserAnswerMapper userAnswerMapper;
    @Autowired
    QuestionDetailsMapper questionDetailsMapper;
    @Autowired
    QuestionMapper questionMapper;

    @Override
    public PageVo<ConstitutionTest> PageConstitution(Integer pageNum, Integer pageSize, String conTestName) {
        QueryWrapper<ConstitutionTest> wrapper = new QueryWrapper<>();
        wrapper.like("con_test_name",conTestName);
        Page page = new Page(pageNum, pageSize);
        IPage iPage = mapper.selectPage(page, wrapper);
        PageVo<ConstitutionTest> constitutionTestPageVo = new PageVo<>();
        constitutionTestPageVo.setRows(iPage.getRecords());
        constitutionTestPageVo.setTotal(iPage.getTotal());
        return constitutionTestPageVo;
    }

    @Override
    public Member queryById(String memName, String memIdentity) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_name",memName);
        wrapper.eq("mem_identity",memIdentity);
        Member member = memberMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public List<Rubric> selectRubric(Integer rubType) {
        List<Rubric> rubrics = rubricMapper.queryRubric(rubType);
        for (Rubric rubric : rubrics) {
            QueryWrapper<RubricOption> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("rub_id",rubric.getRubId());
            List<RubricOption> rubricOptions = optionMapper.selectList(queryWrapper);
            rubric.setOptions(rubricOptions);
        }
        return rubrics;
    }

    @Override
    public void insertUserAnswer(String memId, List<UserAnswer> UserAnswer) {
        for (UserAnswer userAnswer : UserAnswer) {
            QueryWrapper<UserAnswer> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mem_id",memId);
            queryWrapper.eq("rub_id ",userAnswer.getRubId());
            userAnswerMapper.delete(queryWrapper);
            UserAnswer user = new UserAnswer();
            user.setMemId(Integer.parseInt(memId));
            user.setRubId(userAnswer.getRubId());
            user.setRubOpId(userAnswer.getRubOpId());
            userAnswerMapper.insert(user);
        }
    }

    @Override
    public void insertConstitutionTest(String conTestName, Integer memId) {
        QueryWrapper<UserAnswer> userAnswerQueryWrapper = new QueryWrapper<>();
        userAnswerQueryWrapper.eq("mem_id",memId);
        List<UserAnswer> userAnswers = userAnswerMapper.selectList(userAnswerQueryWrapper);
        Integer ph=0;
        Integer qx=0;
        Integer yx=0;
        Integer yxu=0;
        Integer ts=0;
        Integer sr=0;
        Integer xy=0;
        Integer qy=0;
        Integer tp=0;
        //合格分数
        float peh=0;
        float qix=0;
        float yanx=0;
        float yinxu=0;
        float tans=0;
        float shir=0;
        float xuey=0;
        float qiy=0;
        float tep=0;
        for (UserAnswer userAnswer : userAnswers) {
            //获取题目id
            Integer rubId = userAnswer.getRubId();
            //查询题库表 查询题目类型
            Rubric rubric = rubricMapper.selectById(rubId);
            Integer rubType = rubric.getRubType();
            if(rubType==1){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",1);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                peh=quesScore;
                //获取每一个答题分数
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    ph = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    ph=ph+rubOpScore;
                }

            }else if(rubType==2){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",2);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                qix=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    qx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    qx=qx+rubOpScore;
                }

            }else if(rubType==3){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",3);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                yanx=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    yx = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    yx=yx+rubOpScore;
                }
            }else if(rubType==4){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",4);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                yinxu=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    yxu = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    yxu=yxu+rubOpScore;
                }
            }
            else if(rubType==5){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",5);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                tans=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    ts = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    ts=ts+rubOpScore;
                }
            }
            else if(rubType==6){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",6);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                shir=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    sr = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    sr=sr+rubOpScore;
                }
            }
            else if(rubType==7){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",7);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                xuey=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    xy = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    xy=xy+rubOpScore;
                }
            }else if(rubType==8){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",8);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                qiy=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    qy = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    qy=qy+rubOpScore;
                }
            }else if(rubType==9){
                QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("ques_type ",1);
                queryWrapper.eq("ques_test_type ",9);
                queryWrapper.eq("ques_state ",0);
                Question question = questionMapper.selectOne(queryWrapper);
                float quesScore = question.getQuesScore();
                tep=quesScore;
                Integer rubOpId = userAnswer.getRubOpId();
                RubricOption rubricOption = optionMapper.selectById(rubOpId);
                if (rubricOption == null) {
                    tp = 0;
                }else {
                    Integer rubOpScore = rubricOption.getRubOpScore();
                    tp=tp+rubOpScore;
                }
            }
        }
        String conTestPinghe=null;
        String conTestQixu=null;
        String conTestYangxu=null;
        String conTestYinxu=null;
        String conTestTanshi=null;
        String conTestShire=null;
        String conTestXueyu=null;
        String conTestQiyu=null;
        String conTestTebing=null;
        if(ph==0){
            conTestPinghe="未检测";
        }else if(ph<peh){
            conTestPinghe="是";
        }else {
            conTestPinghe="否";
        }

        if(qx==0){
            conTestQixu="未检测";
        }else if(qx<qix){
            conTestQixu="是";
        }else {
            conTestQixu="否";
        }

        if(yx==0){
            conTestYangxu="未检测";
        }else if(yx<yanx){
            conTestYangxu="是";
        }else {
            conTestYangxu="否";
        }

        if(yxu==0){
            conTestYinxu="未检测";
        }else if(yxu<yinxu){
            conTestYinxu="是";
        }else {
            conTestYinxu="否";
        }

        if(ts==0){
            conTestTanshi="未检测";
        }else if(ts<tans){
            conTestTanshi="是";
        }else {
            conTestTanshi="否";
        }

        if(sr==0){
            conTestShire="未检测";
        }else if(sr<shir){
            conTestShire="是";
        }else {
            conTestShire="否";
        }

        if(xy==0){
            conTestXueyu="未检测";
        }else if(xy<xuey){
            conTestXueyu="是";
        }else {
            conTestXueyu="否";
        }

        if(qy==0){
            conTestQiyu="未检测";
        }else if(qy<qiy){
            conTestQiyu="是";
        }else {
            conTestQiyu="否";
        }

        if(tp==0){
            conTestTebing="未检测";
        }else if(tp<tep){
            conTestTebing="是";
        }else {
            conTestTebing="否";
        }

        QueryWrapper<ConstitutionTest> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",memId);
        mapper.delete(wrapper);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format( now );
        ConstitutionTest constitutionTest = new ConstitutionTest();
        constitutionTest.setConTestName(conTestName);
        constitutionTest.setConTestDatetime(time);
        constitutionTest.setConTestPinghe(conTestPinghe);
        constitutionTest.setConTestQixu(conTestQixu);
        constitutionTest.setConTestYangxu(conTestYangxu);
        constitutionTest.setConTestYinxu(conTestYinxu);
        constitutionTest.setConTestTanshi(conTestTanshi);
        constitutionTest.setConTestShire(conTestShire);
        constitutionTest.setConTestXueyu(conTestXueyu);
        constitutionTest.setConTestQiyu(conTestQiyu);
        constitutionTest.setConTestTebing(conTestTebing);
        constitutionTest.setMemId(memId);
        mapper.insert(constitutionTest);
    }

    @Override
    public List<Rubric> queryRubric(Integer quesType, Integer memId) {
        //根据类型 和状态 查找问卷id（问卷表）
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.eq("ques_test_type",quesType);
        wrapper.eq("ques_state",0);
        Question question = questionMapper.selectOne(wrapper);
        Integer quesId = question.getQuesId();
        List<Rubric> rubrics = rubricMapper.selectByquesId(quesId);
        for (Rubric rubric : rubrics) {
            Integer rubId = rubric.getRubId();
            QueryWrapper<UserAnswer> answerQueryWrapper = new QueryWrapper<>();
            answerQueryWrapper.eq("rub_id",rubId);
            answerQueryWrapper.eq("mem_id",memId);
            UserAnswer userAnswer = userAnswerMapper.selectOne(answerQueryWrapper);
            Integer rubOpId = userAnswer.getRubOpId();
            RubricOption rubricOption = optionMapper.selectById(rubOpId);
            rubric.setRubricOption(rubricOption);
        }
        return rubrics;
    }
}
